/**
 *  *2010-3-1 上午10:40:47  add 
 */
package com.base.myproject.client.busi.service.analysis;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.model.BarDataProvider;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.Legend;
import com.extjs.gxt.charts.client.model.LineDataProvider;
import com.extjs.gxt.charts.client.model.ScaleProvider;
import com.extjs.gxt.charts.client.model.Text;
import com.extjs.gxt.charts.client.model.Legend.Position;
import com.extjs.gxt.charts.client.model.charts.BarChart;
import com.extjs.gxt.charts.client.model.charts.LineChart;
import com.extjs.gxt.charts.client.model.charts.BarChart.BarStyle;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.google.gwt.http.client.URL;

/**
 * 
 * 查询消费金额最高的前X位客户，显示最后消费日期
 * @author Administrator
 *2010-3-1
 * javazgw@gmail.com
 */
public class CcodeTopPaidAnalysis extends ContentPanel{
	ChartModel model =null;
	BaseStore<JDBCModelData> store;

	SelectionListener<ButtonEvent> listener ;
	public CcodeTopPaidAnalysis()
	{
		init();
	}
	void init() {
		setWidth("400px");
		initlistener();
		initstore();
		
		initQueryParamPanel();
		initWidget();
		this.setAutoHeight(true);
		//this.setAutoWidth(true);
	}
	void initstore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		// type.addField("b2cgcode");
		type.addField("paid");

		type.addField("ccode");
		type.addField("serviceenddate");

		StoreInfo info = new StoreInfo();


		info.setTablename("sleeping");
		info.setNeedAllRow("no");
		// info.setSql("");
		store = new BaseStore<JDBCModelData>(type, info,false);

	}

	void initWidget() {
		 model = new ChartModel("最活跃的前30名客户",
				"font-size: 16px; font-family: Verdana; text-align: center;");
		model.setBackgroundColour("#fefefe");
		model.setLegend(new Legend(Position.TOP, true));
		model.setScaleProvider(ScaleProvider.ROUNDED_NEAREST_SCALE_PROVIDER);

		
		 BarChart bar = new BarChart(BarStyle.GLASS);
		  bar.setColour("#00aa00"); 
		  BarDataProvider barProvider = new	  BarDataProvider( "paid","ccode","serviceenddate");
		  barProvider.bind(store);
		 bar.setDataProvider(barProvider); 
		// bar.setTooltip("#val#");
		 //bar.addChartListener(listener);
		 model.addChartConfig(bar);
		 
		 LineChart line = new LineChart();
			line.setAnimateOnShow(true);
			line.setText("消费额");
			line.setColour("#ff6600");
			LineDataProvider lineProvider = new LineDataProvider("paid","ccode");
			lineProvider.bind(store);
			line.setDataProvider(lineProvider);
			model.addChartConfig(line);
		 
		 
		 String url = "chart/open-flash-chart.swf";
		 final Chart chart = new Chart(url);
			// chart.setToolTip("点击产品的图型看看");
			chart.setChartModel(model);
			chart.setHeight("300px");
			add(chart);
	}
	void initQueryParamPanel() {
		
		Button thisweek = new Button("查询");
		thisweek.addSelectionListener(listener);
		
		this.addButton(thisweek);
		
		
	}
	void initlistener()
	 {
		 listener = new SelectionListener<ButtonEvent>()
		 {
				public void componentSelected(ButtonEvent ce) {
					// TODO Auto-generated method stub
					
				
					{
						
						StoreInfo info = store.getStoreinfo();
						String str = "select top 30 sum(paid) as paid,(select cname from ccode where ccode.ccode = s_service_po.ccode) as ccode,max(serviceenddate) as serviceenddate from s_service_po where busistatus ='524288' group by ccode order by sum(paid) desc";
					
						info.setSql(str);
						store.reload();
						
						
					}
				}
		 };
		 
	 }
}
